/**
 * 
 */
package com.itinsys.enma.manager.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.itinsys.enma.dao.SchoolDAO;
import com.itinsys.enma.domain.School;
import com.itinsys.enma.domain.response.SchoolResponse;
import com.itinsys.enma.manager.ResponseManager;
import com.itinsys.enma.manager.SchoolManager;

/**
 * @author rperez
 *
 */
public class SchoolManagerImpl implements SchoolManager{

	@Autowired
	private SchoolDAO schoolDAO;
	@Autowired
	private ResponseManager responseManager;
	
	@Override
	public SchoolResponse getAllSchools() {
		SchoolResponse response = new SchoolResponse();
		List<School> schools = schoolDAO.getAll();
		if(schools.isEmpty())
			responseManager.prepareResponse(response, SchoolResponse.SCHOOL_NOT_FOUND);
		else{
			responseManager.prepareResponse(response, SchoolResponse.OK);
			response.setSchools(schools);
		}
		return response;
	}


	@Override
	public SchoolResponse addSchool(School school) {
		
		SchoolResponse response = new SchoolResponse();
			if(schoolDAO.addSchool(school))		
				response=getAllSchools();
			else
				responseManager.prepareResponse(response, SchoolResponse.ERROR);
		return response;
	}

	@Override
	public SchoolResponse updateSchool(School school) {
		SchoolResponse response = new SchoolResponse();
		School schoolToUpdate = schoolDAO.getById(String.valueOf(school.getId()));
		schoolToUpdate.setDescription(school.getDescription());
		
		if(schoolDAO.updateSchool(schoolToUpdate))		
			response=getAllSchools();
		else
			responseManager.prepareResponse(response, SchoolResponse.ERROR);

		return response;
	}

	
	@Override
	public SchoolResponse deleteSchool(School school) {
		SchoolResponse response = new SchoolResponse();
		
		school = schoolDAO.getById(String.valueOf(school.getId()));
		if(schoolDAO.deleteSchool(school))		
			response = getAllSchools();
		else
			responseManager.prepareResponse(response, SchoolResponse.ERROR);
		
		return response;
	}


	@Override
	public School getSchoolById(String schoolId) {
		return schoolDAO.getById(schoolId);
	}

	

}
